﻿// <copyright file="Services.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>

using System.Collections.Generic;

public class Services : UnityEngine.MonoBehaviour
{
    private static Dictionary<System.Type, IService> servicesByType = new Dictionary<System.Type, IService>();

    public static void Register(System.Type key, IService value)
    {
        if (key != null &&
            value != null)
        {
            if (Services.servicesByType.ContainsKey(key))
            {
                return;
            }

            Services.servicesByType.Add(key, value);
        }
    }

    public static void Register<T>(T value) where T : IService
    {
        Services.Register(typeof(T), value);
    }

    public static void Unregister(System.Type key, IService value)
    {
        if (Services.servicesByType.ContainsKey(key))
        {
            Services.servicesByType.Remove(key);
        }
    }

    public static IService GetService(System.Type key)
    {
        if (Services.servicesByType.ContainsKey(key))
        {
            return Services.servicesByType[key];
        }

        return null;
    }

    public static T GetService<T>() where T : IService
    {
        System.Type key = typeof(T);

        if (Services.servicesByType.ContainsKey(key))
        {
            return (T)Services.servicesByType[key];
        }

        return default(T);
    }
}
